package Thread.DateSafety;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * joda的时间转换
 *  线程安全
 */
public class JodaTimeTest {


    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 500; i++) {
            PrintJodaTime pt = new PrintJodaTime();
            pt.threadName = i;
            es.execute(pt);
        }
        es.shutdown();
    }
}


class PrintJodaTime extends Thread {//时间转字符线程
    public int threadName;
    SDFsafetyTest ts = new SDFsafetyTest();
    DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void run() {
        super.run();
        DateTime dateTime = null;
        String strD = null;
        try {
            if (threadName % 2 == 0) {
                strD = "2012-02-02 12:12:12";
            } else {
                strD = "2012-03-30 12:12:12";
            }
             dateTime = DateTime.parse(strD, dateTimeFormat);
        } catch (Exception e) {
        }

        for (int i = 0; i < 100; i++) {
            String sd = dateTime.toString("yyyy-MM-dd HH:mm:ss");
            try {
                Thread.sleep(1);
            } catch (Exception e) {
            }
           // if (!sd.equals("2012-02-02 12:12:12") && !sd.equals("2012-03-30 12:12:12")) {
                System.out.println(threadName + ":  原:" + strD + "  新:" + sd);
           // }
        }
    }

}

